package project_package;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ProductRecords 
{
	private static ProductRecords instance = null;
    HashMap<Integer, Product> products = new HashMap<Integer, Product>();
    int productIndex = 0;
    
    public static ProductRecords getInstance()
    {
    	if(instance == null)
    	{
    		instance = new ProductRecords();
    	}
    	return instance;
    }
        
    // create (add) a new product (to the shop list)
    public boolean createProduct(String name, double price, double discount, String description, int quantity, String location)
    {
    	int oldIndex = productIndex;
        Product newProduct = new Product(productIndex, price, discount, name, description, quantity, location);
        products.put(productIndex, newProduct);
    	productIndex++;
    	
    	return (oldIndex < productIndex) ? true : false;
    }
    
    // delete product
    public boolean deleteProduct(String name)
    {
    	Product product = findProduct(name);
    	if (product != null) 
    	{
    		int id = product.getID();
	        products.remove(id);
	        return true;
    	} 
    	else {
    		return false;
    	}
    }
    
    // updates product info
    public boolean updateProduct(Product product, String name, double price, double discount, String description, int quantity, String location)
    {
    	if (products.containsValue(product)) 
    	{
    		product.setName(name);
    		product.setPrice(price);
    		product.setDiscount(discount);
    		product.setDescription(description);
    		product.setQuantity(quantity);
    		product.setLocation(location);
    		return true;
    	}
    	else {
    		return false;
    	}
    }
    
    // gets product quantity
    public int getQuantity(Product product)
    {
    	return product.getQuantity();
    }
    
    // gets product location    
    public String getLocation(Product product)
    {
    	return product.getLocation();
    }
    
     // searches for a product by name
    public Product findProduct(String name)
    {
    	Product found = null;
    	Set<Map.Entry<Integer, Product>> st = products.entrySet();
    	 
    	for(Map.Entry<Integer, Product> product : st)
    	{  
    	      if (product.getValue().getName().equals(name))
    	    	  found = product.getValue();
    	} 
    	return found;
    }
    
    // print all products
    public void printProducts()
	{
    	Set<Map.Entry<Integer, Product>> st = products.entrySet();
    	for(Map.Entry<Integer,Product> product : st)
    	{
    		System.out.println("Product Name: " + product.getValue().getName());
		}
	}
    
    // total number of products listed
    public int getTotalProducts()
    {
    	int index = products.size();
    	return index;
    }

	public HashMap<Integer, Product> getProducts()
	{
		return products;
	}

	public void changeProductQuantity(Product product, int quantity)
    {
        product.setQuantity(quantity);
    }
}
